<?php

/**
 * This is the model class for table "{{adm_menu}}".
 *
 * The followings are the available columns in table '{{adm_menu}}':
 * @property integer $zam_id
 * @property string $zam_title
 * @property string $zam_url
 * @property integer $zam_parentid
 * @property string $zam_childids
 * @property integer $zam_status
 * 张梦林
 * 2012-12-4
 */
class AdmMenu extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AdmMenu the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{adm_menu}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zam_parentid, zam_status,zam_listorder, zam_type', 'numerical', 'integerOnly'=>true),
			array('zam_title', 'length', 'max'=>25),
			array('zam_url', 'length', 'max'=>255),
			array('zam_childids', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zam_id, zam_title, zam_type, zam_url, zam_parentid, zam_childids, zam_status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'parent'=>array(self::HAS_MANY , 'AdmMenu', array('zam_id'=>'zam_parentid')),//父菜单 
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zam_id' => '菜单ID',
			'zam_title' => '菜单名',
			'zam_type' => '菜单类型',
			'zam_url' => '菜单地址',
			'zam_parentid' => '上级ID',
			'zam_childids' => '子ID',
			'zam_listorder' => '子ID',
			'zam_status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zam_id',$this->zam_id);
		$criteria->compare('zam_title',$this->zam_title,true);
		$criteria->compare('zam_type',$this->zam_type);
		$criteria->compare('zam_url',$this->zam_url,true);
		$criteria->compare('zam_parentid',$this->zam_parentid);
		$criteria->compare('zam_childids',$this->zam_childids,true);
		$criteria->compare('zam_listorder',$this->zam_listorder,true);
		$criteria->compare('zam_status',$this->zam_status);

		$criteria->order = 'zam_listorder ASC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
       		 	'pageSize'=>100,
    		),	
		));
	}
	//返回菜单类型
	public function getType($type=null){
		if($type==null){
			return array(
					'1'=>'多店铺',
					'2'=>'单店铺',
					'3'=>'共用',
					'4'=>'开发人员菜单',
					'5'=>'B2B菜单',
			);
		}else{
			$level = $this->getType();
			return $level[$type];
		}
	}
	
	//返回菜单状态
	public function getStatus($type=null){
		if($type==null){
			return array(
					'1'=>'显示',
					'2'=>'隐藏',
			);
		}else{
			$level = $this->getStatus();
			return $level[$type];
		}
	}
	//返回父级菜单，下拉列表用
	public function getParent($parentId=null){
		$criteria=new CDbCriteria;
		$parent = $this->findAll($criteria);
		//print_r($parent);exit;
		if(!empty($parent)){
			$parent = simplifyData($parent);
			if(isset($parentId)){
				foreach($parent as $val){
					if($val['zam_parentid'] = $parentId) return $val['zam_title'];
				}
			}else{
				$parentarr[0]="一级分类";
				foreach($parent as $val){
					$parentarr[$val['zam_id']] = $val['zam_title'];
				}
				return $parentarr;
			}
		}else{
			return array();
		}
	}
	//返回一级菜单列表
	public function getleve1Menu(){
		$criteria=new CDbCriteria;
		$criteria->compare('zam_parentid',0);
		$criteria->compare('zam_status',1);//print_r($this->getAdmMenuYypes());
		$criteria->addInCondition('zam_type',$this->getAdmMenuYypes());
		$criteria->order = 'zam_listorder ASC';
		$parent = $this->findAll($criteria);//print_r($parent);
		
		//查找活动控制器菜单ID
		$criteria=new CDbCriteria;
		$criteria->compare('zam_parentid','>0');
	//	$criteria->addInCondition('zam_type',$this->getAdmMenuYypes());
		$criteria->compare('zam_url','/manage/'.Yii::app()->getController()->id.'/',true);
		$activeAction =  $this->find($criteria);
		//查找活动控制器对应的顶级菜单ID
		$actionParent = $this->findByPk($activeAction->zam_parentid);
		if($actionParent->zam_parentid==0){
			$activeTopid = $actionParent->zam_id;
		}else{
			$actionTop = $this->findByPk($activeAction->zam_parentid);
			$activeTopid = $actionTop->zam_id;
		}

		if(!empty($parent)){
			$parent = simplifyData($parent);
			//添加首页
			$menu[] = array(
						'label'=>'',
						'itemOptions'=>array('class'=>'man_bg nav_l'),
						);
			$menu[] = array(
						'label'=>t('Home','adm'), 
						'url'=>url('/manage/index/index'),
						'itemOptions'=>array('class'=>'first item'),
						);

			foreach($parent as $val){
				$menu[] = array(
						'label'=>$val['zam_title'], 
						'url'=>$val['zam_url'],
						'active'=>($val['zam_id']==$activeTopid),
						);
			}
			return $menu;
		}
	}
	//返回二级菜单列表
	public function getleve2Menu(){
		//查找活动控制器菜单ID
		$criteria=new CDbCriteria;
		$criteria->compare('zam_parentid','>0');
	//	$criteria->addInCondition('zam_type',$this->getAdmMenuYypes());
		$criteria1 = clone $criteria;
		$criteria1->compare('zam_url','/manage/'.Yii::app()->getController()->id.'/',true);
		//如果一个控制器下存在多个菜单，则需细分
		if($this->count($criteria1)>1){
			$criteria2 = clone $criteria;
			$criteria2->compare('zam_url','/manage/'.Yii::app()->getController()->id.'/'.Yii::app()->getController()->active_action,true);
			$activeAction =  $this->find($criteria2);
			if(empty($activeAction)){
				$activeAction =  $this->find($criteria1);
			}
		}else{
			$activeAction =  $this->find($criteria1);
		}
		if(!empty($activeAction)){
			//如果是一级菜单，取子菜单
				//查找活动控制器对应的顶级菜单ID
				$actionParent = $this->findByPk($activeAction->zam_parentid);
				if($actionParent->zam_parentid==0){
					$activeTopid = $actionParent->zam_id;
				}else{
					$activeTopid = $actionParent->zam_parentid;
				}
			//查找一级分类下对应的子菜单
			$criteria=new CDbCriteria;
			$criteria->compare('zam_parentid',$activeTopid);
			$criteria->compare('zam_status',1);
			$criteria->order = 'zam_listorder ASC';
			$criteria->addInCondition('zam_type',$this->getAdmMenuYypes());
			$leve2 = $this->findAll($criteria);

			//遍历结果，生成子菜单。并返回
			foreach($leve2 as $key=>$val){
				//二级菜单
				$menu[$key]['title'] = $val->zam_title;
				//三级菜单
				$criteria=new CDbCriteria;
				$criteria->compare('zam_parentid',$val->zam_id);
				$criteria->compare('zam_status',1);
				$criteria->addInCondition('zam_type',$this->getAdmMenuYypes());
				$criteria->order = 'zam_listorder ASC';
				$leve3 = $this->findAll($criteria);
				foreach($leve3 as $val3){
					$menu[$key]['items'][] = array(
							'label'=>$val3->zam_title, 
							'url'=>$val3->zam_url,
							'active'=>($val3->zam_id==$activeAction->zam_id),
					);
				}
			}
			return $menu;
		}else{
			return array();
		}
	}
	
	//根据店铺类型返回拥有的后台菜单类型
	public function getAdmMenuYypes(){
		//店铺类型：1单店铺，2多店铺， 3单店铺多店铺复合
		$sitetype =  F::sitetype();
		if($sitetype==1){
			$type[]=1;
		}elseif($sitetype==2){
			$type[]=2;
		}elseif($sitetype==3){
			$type[]=1;
			$type[]=2;
		}
		//共用菜单
		$type[]=3;
		//超级管理员拥有开发人员菜单
		if(Yii::app()->user->getName()==F::superadminer()){
			$type[]=4;
		}
		return $type;
	}
	
	//返回一级菜标题
	public function getleve1Title($actionId){
		//查找控制器菜单ID
		$criteria=new CDbCriteria;
		$criteria->compare('zam_url','/manage/'.$actionId,true);
		$admMenu =  $this->find($criteria);
		if(!empty($admMenu)){
			//如果是一级菜单，取该菜单名称，如果不是一级菜单，取对应一级菜单名称
			if($admMenu->zam_parentid==0){
				$parentId = $admMenu->zam_id;
			}else{
				$parentId = $admMenu->zam_parentid;
			}
			//根据父ID查找父菜单
			$criteria=new CDbCriteria;
			$criteria->compare('zam_id',$parentId);
			$admMenuParent =  $this->find($criteria);
			if(!empty($admMenu)){
				//返回父菜单名称
				return $admMenuParent->zam_title;
			}
		}
	}

}